package com.whq.mvc.base.algorithm;

/**
 * 盛最多水的容器
 * 力扣地址：https://leetcode.cn/problems/container-with-most-water/
 */
public class A1 {

    /**
     * 穷举的方法，全部遍历一遍
     *
     * @param data
     * @return
     */
    public static int method0(int[] data) {
        int maxArea = 0;
        for (int i = 0; i < data.length - 1; i++) {
            for (int j = i + 1; j < data.length; j++) {
                int area = (j - i) * Math.min(data[i], data[j]);
                maxArea = Math.max(area, maxArea);
            }
        }
        return maxArea;
    }

    /**
     * 双指针方法
     * @param data
     * @return
     */
    public static int method1(int[] data) {
        int maxArea = 0;
        for (int i = 0, j = data.length - 1; i < j; ) {
            if (data[i] < data[j]){
                i++;
            }else {
                j--;
            }
            int h = Math.min(data[i],data[j]);
            int area = (j - i) * h;
            maxArea = Math.max(area, maxArea);

        }
        return maxArea;
    }

    public static void main(String[] args) {
        int[] data = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        long start = System.currentTimeMillis();
        int max = method1(data);
        long end = System.currentTimeMillis();
        System.out.println(max);
        System.out.printf("耗时：%d毫秒", end - start);
    }
}
